﻿@inherits AdminCompontentBase
@page "/messageRecords/messageRecordManagement"

<PageTitle>@T("Permission.MessageRecord")</PageTitle>
<AutoHeight AutoClass="mt-6">
    <HeaderContent>
        <SSearchPanel @bind-Value="@_queryParam.Filter" Advanced OnEnter="RefreshAsync" OnClearClick="HandleClearAsync">
            <ChildContent>
                <DefaultTitle>@T("Title")</DefaultTitle>
            </ChildContent>
            <AdvancedContent>
                <MCol Md="2">
                    <SSelect Small @bind-Value="@_queryParam.TimeType"
                             Items="@(GetEnumList<MessageRecordTimeTypes>())"
                             Label="@T("DisplayName.MessageRecordTimeTypes")"
                             ItemText="@(item => T($"DisplayName.MessageRecordTimeTypes.{item.ToString()}"))"
                             ItemValue="item => item"
                             BackgroundColor="white"
                             TItem="MessageRecordTimeTypes"
                             TItemValue="MessageRecordTimeTypes"
                             TValue="MessageRecordTimeTypes?"
                             Class="rounded-lg body2"
                             OnSelectedItemUpdate="RefreshAsync">
                    </SSelect>
                </MCol>
                <MCol Class="flex-grow-0">
                    <SDateTimeRangeToolbar StartDateTime="_startTime" EndDateTime="_endTime" OnUpdate="DateRangChangedAsync" ShowQuickChange="false" />
                </MCol>
                <MCol Md="2">
                    <SSelect Small @bind-Value="_queryParam.ChannelId"
                             Items="@_channelItems"
                             Label="@T("DisplayName.Channel")"
                             ItemText="item => item.DisplayName"
                             ItemValue="item => item.Id"
                             BackgroundColor="white"
                             Clearable
                             HideDetails="@("auto")"
                             TItem="ChannelDto"
                             TItemValue="Guid"
                             TValue="Guid?"
                             Class="rounded-lg body2"
                             OnClearClick="RefreshAsync"
                             OnSelectedItemUpdate="RefreshAsync">
                    </SSelect>
                </MCol>
                <MCol Md="2">
                    <UserSelect @bind-Value="_queryParam.UserId" Label="@T("DisplayName.MessageRecordUserId")" OnClearClick="RefreshAsync"
                                OnSelectedItemUpdate="RefreshAsync" />
                </MCol>
                <MCol Md="2">
                    <BooleanSelect @bind-Value="_queryParam.Success" Items="_successItems" Label="@T("DisplayName.MessageRecordSuccess")" OnClearClick="RefreshAsync" OnSelectedItemUpdate="RefreshAsync" />
                </MCol>
            </AdvancedContent>
        </SSearchPanel>
    </HeaderContent>
    <AutoHeightContent>
        <MCard Class="full-height">
            <SDataTable Headers="GetHeaders()" Items="_entities.Result" TItem="MessageRecordDto" ItemsPerPage="_queryParam.PageSize" HideDefaultFooter FixedRight Class="d-flex full-height flex-column">
                <HeaderColContent Context="header">
                    <span class="text-btn">@header.Text</span>
                </HeaderColContent>
                <ItemColContent>
                    @switch (context.Header.Value)
                    {
                        case ("Receiver"):
                            <MListItem Class="px-0 text-truncate">
                                <MListItemAvatar Class="mr-3">
                                    <DefaultUserAvatar Avatar="@context.Item.User.Avatar" Size="40" />
                                </MListItemAvatar>
                                <MListItemContent>
                                    <MListItemTitle class="subtitle emphasis--text">
                                        @(!string.IsNullOrEmpty(context.Item.User.DisplayName) ? context.Item.User.DisplayName : !string.IsNullOrEmpty(context.Item.User.PhoneNumber) ? context.Item.User.PhoneNumber : context.Item.User.Account)
                                    </MListItemTitle>
                                    <MListItemSubtitle class="caption regular2--text">@(!string.IsNullOrEmpty(context.Item.User.DisplayName) && !string.IsNullOrEmpty(context.Item.User.PhoneNumber) ? context.Item.User.PhoneNumber : context.Item.User.Account)</MListItemSubtitle>
                                    </MListItemContent>
                                </MListItem>
                            break;
                        case (nameof(MessageTaskReceiverDto.Email)):
                            <DefaultGenericColumnRender Value="@(context.Item.User.Email)" Width="@context.Header.Width"></DefaultGenericColumnRender>
                            break;
                        case ("ChannelDisplayName"):
                            <DefaultGenericColumnRender Value="@context.Item.Channel?.DisplayName" Width="@context.Header.Width"></DefaultGenericColumnRender>
                            break;
                        case nameof(MessageRecordDto.Success):
                            <MessageRecordStatusTag Class="text-truncate" Value="@context.Item.Success" />
                            break;
                        case "Action":
                            <DefaultIcon Small Color="emphasis2" Tooltip="@(T("View"))" OnClick="async()=>await _detailModal.OpenModalAsync(context.Item)">mdi-eye</DefaultIcon>
                            break;
                        default:
                            <DefaultGenericColumnRender Value="@context.Value" Width="@context.Header.Width"></DefaultGenericColumnRender>
                            break;
                    }
                </ItemColContent>
                <FooterContent>
                    <SPagination Class="mt-3 mx-6 pb-6" Page="@_queryParam.Page" PageSize="@_queryParam.PageSize" Total="_entities.Total" PageChanged="HandlePageChanged" PageSizeChanged="HandlePageSizeChanged" />
                </FooterContent>
            </SDataTable>
        </MCard>
    </AutoHeightContent>
</AutoHeight>

<MessageRecordDetailModal @ref="_detailModal" OnOk="HandleOk" />

